/**
 * 
 */
package controllers;

import java.util.ArrayList;

import client.gui.panels.PanelIF;
import client.gui.panels.TicketsTablePanel;
import entities.Message;
import entities.MsgType;
import entities.Ticket;
import entities.TimeExtension;
import entities.User;

/**
 * @author Vodi
 *
 */
public class TicketsTableController implements ControllerIF {

	/**
	 * Reference of the controller panel
	 */
	private TicketsTablePanel ticketsTablePanel;
	
	/**
	 * Reference of the main panel controller
	 */
	private MainPanelController mainPanelController;
	
	
	public TicketsTableController(MainPanelController mainPanelController) {
		this.mainPanelController = mainPanelController;
	}
	
	/* (non-Javadoc)
	 * @see controllers.ControllerIF#getPanel()
	 */
	@Override
	public PanelIF getPanel() {
		return this.ticketsTablePanel;
	}

	@Override
	public void setPanel(PanelIF panel) {
		this.ticketsTablePanel = (TicketsTablePanel) panel;
	}
	
	/* (non-Javadoc)
	 * @see controllers.ControllerIF#action()
	 */
	@Override
	public void action() {
		mainPanelController.ticketDetailsRequest(ticketsTablePanel.getSelectedTicket().getTicket_id());
	}
	
	public void sendMessage(Message message){
		mainPanelController.sendMessage(message);
	}

	/* (non-Javadoc)
	 * @see controllers.ControllerIF#decipherMessage(entities.Message)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void decipherMessage(Message message) {
		if(message.getMsgType().equals(MsgType.TICKET_LIST_RESPONSE)) {
			ticketsTablePanel.fillTableData((ArrayList<Ticket>) message.getObject());
		}
		else if(message.getMsgType().equals(MsgType.LIST_ALL_USERS_RESPONSE)) {
			ticketsTablePanel.fillTableDataWithUsers((ArrayList<User>) message.getObject());
		}
		else if(message.getMsgType().equals(MsgType.LIST_TIME_EXTENSTIONS_REQUESTS_RESPONSE)) {
			ticketsTablePanel.fillTableDataWithExtensions((ArrayList<TimeExtension>) message.getObject());
		}
		else {
			if(message.getMsgType().equals(MsgType.TICKET_LIST_ERR) || message.getMsgType().equals(MsgType.LIST_ALL_USERS_ERR) || message.getMsgType().equals(MsgType.LIST_TIME_EXTENSTIONS_REQUESTS_ERR)) {
				mainPanelController.showErrorMessage("Error: unable to fetch information");
			}
			else if(message.getMsgType().equals(MsgType.TICKET_LIST_EMPTY) || message.getMsgType().equals(MsgType.LIST_ALL_USERS_EMPTY) || message.getMsgType().equals(MsgType.LIST_TIME_EXTENSTIONS_REQUESTS_EMPTY)) {
				mainPanelController.showSplashMessage("No Information Found");
			}
		}
	}

}
